﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Data.SqlClient;

namespace QLNS_Client
{
    public partial class frm_ThemDT : Form
    {
        string temp = "";
        string flag = "";
        ServiceReference1.Service_QLNSClient sv = new ServiceReference1.Service_QLNSClient();
        public frm_ThemDT()
        {
            InitializeComponent();
        }

        private void frm_ThemDT_Load(object sender, EventArgs e)
        {
            Load_Data();   
        }
        public void Load_Data()
        {
          dr_ds_DT.DataSource = sv.getDT();
          btn_LuuDT.Enabled = false;
          btn_HuyDT.Enabled = false;
        }

        private void btn_ThemDT_Click(object sender, EventArgs e)
        {
            flag = "them";
            btn_ThemDT.Enabled = true;
            btn_LuuDT.Enabled = true;
            btn_HuyDT.Enabled = true;
            btn_SuaDT.Enabled = false;
            btn_XoaDT.Enabled = false;
            btn_Thoat_DT.Enabled = true;
            txt_MaDT.Enabled = true;
            txt_TenDT.Enabled = true;
            txt_MaDT.Text = "";
            txt_TenDT.Text = "";
            txt_MaDT.Focus();
            
        }

        private void btn_ThoatDT_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn thật sự muốn thoát khỏi bảng này?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                this.Close();
            }
        }
       
        private void btn_XoaDT_Click(object sender, EventArgs e)
        {
            ServiceReference1.DAN_TOC dt = new ServiceReference1.DAN_TOC();
            dt.Ma_DT = Convert.ToInt32(temp);
            DialogResult dr = MessageBox.Show("Bạn thật sự muốn xóa dòng này?", "warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                sv.deleteDT(dt);
                Load_Data();
                txt_MaDT.Text = "";
                txt_TenDT.Text = "";
            }
        }

        
        private void dr_ds_DT_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txt_MaDT.Text = dr_ds_DT.CurrentRow.Cells[0].Value.ToString();
            txt_TenDT.Text = dr_ds_DT.CurrentRow.Cells[1].Value.ToString();
            temp = dr_ds_DT.CurrentRow.Cells[0].Value.ToString();
            txt_MaDT.Enabled = false;
            txt_TenDT.Enabled = false;
        }
        private void btn_SuaDT_Click(object sender, EventArgs e)
        {
            flag = "sua";
            btn_HuyDT.Enabled = true;
            btn_LuuDT.Enabled = true;
            txt_TenDT.Enabled = true;
        }

        private void btn_LuuDT_Click(object sender, EventArgs e)
        {
            string a = txt_MaDT.Text;
            string b = txt_TenDT.Text;
            ServiceReference1.DAN_TOC dt = new ServiceReference1.DAN_TOC();
            dt.Ma_DT = Convert.ToInt32(a);
            dt.TenDT = b;

            if (a == "" | b == "")
            {
                MessageBox.Show("Nhập đầy đủ dữ liệu!");
            }
            else
            {
                int kq = 0;
                if (flag == "them")
                {
                    if (sv.check_dt(dt))
                        kq = -1;
                    else
                        kq = sv.addDT(dt);
                }
                else
                {
                    if (flag == "sua")
                    {
                        sv.setDT(dt);
                        kq = 1;
                    }
                }
                if (kq != -1)
                {
                    MessageBox.Show("Hoàn tất!");
                    Load_Data();
                    btn_SuaDT.Enabled = true;
                    btn_XoaDT.Enabled = true;
                }
                else
                {
                    MessageBox.Show("Lôi!!!");
                }
            }
        }
        private void btn_HuyDT_Click(object sender, EventArgs e)
        {
            btn_HuyDT.Enabled = false;
            btn_LuuDT.Enabled = false;
            btn_SuaDT.Enabled = true;
            btn_XoaDT.Enabled = true;
        }
    }
}
